/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.tnmk.worktime.service.model.xmlconverter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.tnmk.worktime.service.dao.entry.SessionEntry;
import org.tnmk.worktime.service.dao.entry.SpendEntry;
import org.tnmk.worktime.service.model.xmladapter.ListWrapper;

/**
 *
 * @author Administrator
 */
public class XmlUtils {
  private static JAXBContext CONTEXT;
  protected static JAXBContext getContext(){
    try {
      if (CONTEXT == null) {
        CONTEXT = JAXBContext.newInstance(SessionEntry.class,ListWrapper.class, SpendEntry.class);
      }
    } catch (JAXBException ex) {
//      Logger.getLogger(XmlUtils.class.getName()).log(Level.SEVERE, null, ex);
      ex.printStackTrace();
    }
    return CONTEXT;
  }
  protected static Marshaller getMarshall(){
    Marshaller m = null;
    try {
      JAXBContext context = getContext();
      m = context.createMarshaller();
      m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
      m.setProperty("jaxb.encoding", "US-ASCII");
    } catch (JAXBException ex) {
//      Logger.getLogger(XmlUtils.class.getName()).log(Level.SEVERE, null, ex);
      ex.printStackTrace();
    }
    return m;
  }
  protected static Unmarshaller getUnmarshall(){
    Unmarshaller m = null;
    try {
      JAXBContext context = getContext();
      m = context.createUnmarshaller();
//      m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    } catch (JAXBException ex) {
//      Logger.getLogger(XmlUtils.class.getName()).log(Level.SEVERE, null, ex);
      ex.printStackTrace();
    }
    return m;
  }
  public static String marshall(Object model){
    try {
      Marshaller m = getMarshall();
      StringWriter sw = new StringWriter();
      m.marshal(model, sw);
      return sw.toString();
    } catch (JAXBException ex) {
//      Logger.getLogger(XmlUtils.class.getName()).log(Level.SEVERE, null, ex);
      ex.printStackTrace();
    }
    return null;
  }
  public static String marshallToFile(Object model, File filePath){
    try {
      Marshaller m = getMarshall();
      FileWriter writer = new FileWriter(filePath, false);
      m.marshal(model, writer);
      return writer.toString();
    } catch (JAXBException | IOException ex) {
//      Logger.getLogger(XmlUtils.class.getName()).log(Level.SEVERE, null, ex);
      ex.printStackTrace();
    }
    return null;
  }
  public static Object unmarshallFromFile(File file) {
    if (file == null || !file.exists() || !file.isFile() ) {
      return null;
    }
    try {
      Unmarshaller m = getUnmarshall();
      FileReader reader = new FileReader(file);
      Object rs = m.unmarshal(reader);
      return rs;
    } catch (JAXBException | FileNotFoundException ex) {
//      Logger.getLogger(XmlUtils.class.getName()).log(Level.SEVERE, null, ex);
      ex.printStackTrace();
    }
    return null;
  }
  public static Object unmarshall(String str) {
    if (str == null || str.isEmpty()) {
      return null;
    }
    try {
      Unmarshaller m = getUnmarshall();
//      m.setProperty("jaxb.encoding", "UTF-8");
      StringReader reader = new StringReader(str);
      Object rs = m.unmarshal(reader);
      return rs;
    } catch (JAXBException ex) {
//      Logger.getLogger(XmlUtils.class.getName()).log(Level.SEVERE, null, ex);
      ex.printStackTrace();
    }
    return null;
  }
}
